package com.iamteer.day17;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

import com.iamteer.util.JdbcUtil;

public class StatementEg {
	private String url = "jdbc:mysql://localhost:3306/day17";
	private String user = "root";
	private String password = "123456";
	
	//Statement 执行 DDL
	@Test
	public void test1() {
		Statement stmt = null;
		Connection conn = null;
		
		try {
			//1 注册驱动程序
			Class.forName("com.mysql.jdbc.Driver");
			
			//2 获取连接对象
			conn = DriverManager.getConnection(url, user, password);
			
			//3 创建 Statement
			stmt = conn.createStatement();
			
			//4 准备 sql
			String sql = "CREATE TABLE student(id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(20),gender VARCHAR(2));";
			
			//5 发送 sql 语句，执行 sql 语句，返回结果
			int count = stmt.executeUpdate(sql);
			
			System.out.println("Affected " + count + " lines");
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
		}
	}
	
	//Statement 执行 DML——增加
	@Test
	public void testInsert() {
		Statement stmt = null;
		Connection conn = null;
		
		try {
			//1 注册驱动程序
			Class.forName("com.mysql.jdbc.Driver");
			
			//2 获取连接对象
			conn = DriverManager.getConnection(url, user, password);
			
			//3 创建 Statement
			stmt = conn.createStatement();
			
			//4 准备 sql
			String sql = "INSERT INTO student(name,gender) VALUES('李四','男');";
			
			//5 发送 sql，执行 sql，返回执行结果
			int count = stmt.executeUpdate(sql);
			
			System.out.println("Affected " + count + " rows");
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
		}
	}

	//Statement 执行 DML——更新
	@Test
	public void testUpdate(){
		Statement stmt = null;
		Connection conn = null;
		
		try {
			//1 注册驱动程序
			Class.forName("com.mysql.jdbc.Driver");
			
			//2 获取连接对象
			conn = DriverManager.getConnection(url, user, password);
			
			//3 创建 Statement 对象
			stmt = conn.createStatement();
			
			//4 准备 sql 语句
			String sql = "UPDATE student SET gender='女' WHERE id=1;";
			
			//5 发送 sql，执行 sql，返回结果
			int count = stmt.executeUpdate(sql);
			System.out.println("Affected " + count + " rows");
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
		}
	}

	//Statement 执行 DML——删除
	@Test
	public void testDelete() {
		Statement stmt = null;
		Connection conn = null;
		
		try {
//			//1 注册驱动程序
//			Class.forName("com.mysql.jdbc.Driver");
//			
//			//2 获取连接对象
//			conn = DriverManager.getConnection(url, user, password);

			conn = JdbcUtil.getConnection();
			//3 创建 Statement 对象
			stmt = conn.createStatement();
			
			//4 准备 sql
			String sql = "DELETE FROM student WHERE id=1;";
			
			//5 发送 sql，执行 sql，返回结果
			int count = stmt.executeUpdate(sql);
			System.out.println("Affected " + count + " rows");
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
/*			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}*/
			JdbcUtil.close(conn, stmt);
		}
	}

	//Statement 执行 DQL——
	@Test
	public void testQuery() {
		Statement stmt = null;
		Connection conn = null;
		ResultSet rs = null;
		try {
			//1 获取连接对象
			conn = JdbcUtil.getConnection();
			//2 创建 Statement 对象
			stmt = conn.createStatement();
			
			//3 准备 sql
			String sql = "SELECT * FROM student";
			
			//4 发送 sql，执行 sql，返回结果
			rs = stmt.executeQuery(sql);
			
			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String gender = rs.getString("gender");
				System.out.println(id + " " + name + " " + gender);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			
			JdbcUtil.close(conn, stmt,rs);
		}
	}

}
